using UnityEngine;
using Unity.Cinemachine;
using RootMotion.FinalIK;
using Unity.Robotics.UrdfImporter;





#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
namespace StarterAssets
{
	public class StarterAssetsInputs : MonoBehaviour
	{
		[Header("Character Input Values")]
		public Vector2 move;
		public Vector2 look;
		public bool jump;
		public bool sprint;

		[Header("Movement Settings")]
		public bool analogMovement;

		[Header("Mouse Cursor Settings")]
		public bool cursorLocked = true;
		public bool cursorInputForLook = true;

		#region 一三人称切换
		[SerializeField] private CinemachineCamera firstCinemachineCamera;
		[SerializeField] private CinemachineCamera thirdCinemachineCamera;
		public bool isThirdView = true;

		//Final Ik动画交互控制
		[SerializeField] private InteractionSystem interactionSystem;
		[SerializeField] private InteractionObject interactionObject_Button;

		#endregion
		public PlayerInput playerInput;
		public Camera RobotCamera;
		public static bool isRobotCameraEnabled = false;
        void Awake()
        {
			interactionSystem=GetComponent<InteractionSystem>();
        }
        private void Start()
		{
			playerInput.actions.FindActionMap("UI").Enable();
		}

#if ENABLE_INPUT_SYSTEM
		public void OnMove(InputValue value)
		{
			MoveInput(value.Get<Vector2>());
		}

		public void OnLook(InputValue value)
		{
			if (cursorInputForLook)
			{
				LookInput(value.Get<Vector2>());
			}
		}

		public void OnJump(InputValue value)
		{
			JumpInput(value.isPressed);
		}

		public void OnSprint(InputValue value)
		{
			SprintInput(value.isPressed);
		}
		public void OnSwitchView(InputValue value)
		{
			isThirdView = !isThirdView;
			if (isThirdView == true)
			{
				thirdCinemachineCamera.Priority = 1;
				firstCinemachineCamera.Priority = 0;
			}
			else
			{
				thirdCinemachineCamera.Priority = 0;
				firstCinemachineCamera.Priority = 1;
			}
		}
		public void OnInteract(InputValue value)
		{
			interactionSystem.StartInteraction(FullBodyBipedEffector.RightHand,interactionObject_Button,true);
		}
		public void OnOpenSettingPanel()
		{
			if (UIMgr.Instance.GetPanel<SettingPanel>() == null || UIMgr.Instance.GetPanel<SettingPanel>().gameObject.activeSelf == false)
			{
				UIMgr.Instance.ShowPanel<SettingPanel>();
				ShowCursor();
			}
			else
			{
				UIMgr.Instance.HidePanel<SettingPanel>();
				HideCursor();
			}
		}
		public void OnChangeRobotView()
		{
			RobotCamera.enabled = !RobotCamera.enabled;
			isRobotCameraEnabled = !isRobotCameraEnabled;
		}

#endif


		public void MoveInput(Vector2 newMoveDirection)
		{
			move = newMoveDirection;
		}

		public void LookInput(Vector2 newLookDirection)
		{
			look = newLookDirection;
		}

		public void JumpInput(bool newJumpState)
		{
			jump = newJumpState;
		}

		public void SprintInput(bool newSprintState)
		{
			sprint = newSprintState;
		}
		public static void HideCursor()
		{
			Cursor.visible = false;
			Cursor.lockState = CursorLockMode.Locked;
		}
		public static void ShowCursor()
		{
			Cursor.visible = true;
			Cursor.lockState = CursorLockMode.None;
		}
		// private void OnApplicationFocus(bool hasFocus)
		// {
		// 	SetCursorState(cursorLocked);
		// }

		// private void SetCursorState(bool newState)
		// {
		// 	Cursor.lockState = newState ? CursorLockMode.Locked : CursorLockMode.None;
		// }
	}
	
}